<?php
declare(strict_types=1);
namespace app\tjy_v1\util\key;
// Rsa签名
class Rsa
{
    public static function index()
    {
        return '这是一个:非对称加密RSA签名处理类';
    }
    public static function rsaSign(string $privateKeyStr, string $content): string
    {
        //生成签名 
        openssl_sign($content, $signature, self::pemFormat($privateKeyStr, 'PRIVATE KEY'), OPENSSL_ALGO_SHA256);
        return base64_encode($signature);
    }
    public static function rsaVerify(string $RSA_merchants, string $content, string $signature): bool
    {
        //校验签名
        return openssl_verify($content, base64_decode($signature), self::pemFormat($RSA_merchants, 'PUBLIC KEY'), OPENSSL_ALGO_SHA256) === 1;
    }
    private static function pemFormat(string $keyStr, string $label): string
    {
        // 格式化密钥
        return "-----BEGIN {$label}-----\n" . chunk_split($keyStr, 64) . "-----END {$label}-----\n";
    }
}
